Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
strolly
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dnns01
strolly
Commits
5d91dc48
Commit
5d91dc48
authored
4 years ago
by
dnns01
Browse files
Options
Downloads
Patches
Plain Diff
Added !poll command to start a poll in a channel.
parent
1b895a76
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fernuni-bot.py
+20
-0
20 additions, 0 deletions
fernuni-bot.py
poll.py
+80
-0
80 additions, 0 deletions
poll.py
with
100 additions
and
0 deletions
fernuni-bot.py
+
20
−
0
View file @
5d91dc48
...
...
@@ -5,6 +5,8 @@ import discord
from
discord.ext
import
commands
from
dotenv
import
load_dotenv
from
poll
import
Poll
# .env file is necessary in the same directory, that contains several strings.
load_dotenv
()
TOKEN
=
os
.
getenv
(
'
DISCORD_TOKEN
'
)
...
...
@@ -158,6 +160,11 @@ async def cmd_add_role(ctx, key, role):
await
send_dm
(
ctx
.
author
,
f
"
Fehler beim Hinzufügen der Rolle
{
role
}
"
)
@bot.command
(
name
=
"
poll
"
)
async
def
cmd_poll
(
ctx
,
question
,
*
answers
):
await
Poll
(
bot
,
question
,
answers
,
ctx
.
author
.
id
).
send_poll
(
ctx
)
def
load_roles
():
global
assignable_roles
roles_file
=
open
(
ROLES_FILE
,
mode
=
'
r
'
)
...
...
@@ -251,10 +258,23 @@ async def on_ready():
@bot.event
async
def
on_raw_reaction_add
(
payload
):
if
payload
.
user_id
==
bot
.
user
.
id
:
return
if
payload
.
emoji
.
name
==
PIN_EMOJI
:
channel
=
await
bot
.
fetch_channel
(
payload
.
channel_id
)
message
=
await
channel
.
fetch_message
(
payload
.
message_id
)
await
pin_message
(
message
)
elif
payload
.
emoji
.
name
in
[
"
🗑️
"
,
"
🛑
"
]:
channel
=
await
bot
.
fetch_channel
(
payload
.
channel_id
)
message
=
await
channel
.
fetch_message
(
payload
.
message_id
)
if
len
(
message
.
embeds
)
>
0
and
message
.
embeds
[
0
].
title
==
"
Umfrage
"
:
poll
=
Poll
(
bot
,
message
=
message
)
if
str
(
payload
.
user_id
)
==
poll
.
author
:
if
payload
.
emoji
.
name
==
"
🗑️
"
:
await
poll
.
delete_poll
()
else
:
await
poll
.
close_poll
()
@bot.event
...
...
This diff is collapsed.
Click to expand it.
poll.py
0 → 100644
+
80
−
0
View file @
5d91dc48
import
discord
OPTIONS
=
[
"
\u0031\u20E3
"
,
"
\u0032\u20E3
"
,
"
\u0033\u20E3
"
,
"
\u0034\u20E3
"
,
"
\u0035\u20E3
"
,
"
\u0036\u20E3
"
,
"
\u0037\u20E3
"
,
"
\u0038\u20E3
"
,
"
\u0039\u20E3
"
,
"
\u0040\u20E3
"
]
class
Poll
:
def
__init__
(
self
,
bot
,
question
=
None
,
answers
=
None
,
author
=
None
,
message
=
None
):
self
.
bot
=
bot
self
.
question
=
question
self
.
answers
=
answers
self
.
author
=
author
if
message
:
self
.
message
=
message
self
.
answers
=
[]
embed
=
message
.
embeds
[
0
]
self
.
author
=
embed
.
fields
[
0
].
value
[
3
:
-
1
]
self
.
question
=
embed
.
description
for
i
in
range
(
2
,
len
(
embed
.
fields
)):
self
.
answers
.
append
(
embed
.
fields
[
i
].
value
)
async
def
send_poll
(
self
,
channel
,
result
=
False
):
option_ctr
=
0
title
=
"
Umfrage
"
if
result
:
title
+=
"
Ergebnis
"
if
len
(
self
.
answers
)
>
10
:
channel
.
send
(
"
Fehler beim Erstellen der Umfrage! Es werden derzeit nicht mehr als 10 Optionen unterstützt!
"
)
return
embed
=
discord
.
Embed
(
title
=
title
,
description
=
self
.
question
)
embed
.
add_field
(
name
=
"
Erstellt von
"
,
value
=
f
'
<@!
{
self
.
author
}
>
'
,
inline
=
False
)
embed
.
add_field
(
name
=
"
\u200b
"
,
value
=
"
\u200b
"
,
inline
=
False
)
for
i
in
range
(
0
,
len
(
self
.
answers
)):
name
=
f
'
{
OPTIONS
[
i
]
}
'
value
=
f
'
{
self
.
answers
[
i
]
}
'
if
result
:
reaction
=
self
.
get_reaction
(
OPTIONS
[
i
])
if
reaction
:
name
+=
f
'
:
{
reaction
.
count
-
1
}
'
value
+=
f
'
\n
Stimmen:
'
async
for
user
in
reaction
.
users
():
if
self
.
bot
.
user
==
user
:
continue
value
+=
f
'
<@!
{
str
(
user
.
id
)
}
>
'
embed
.
add_field
(
name
=
name
,
value
=
value
,
inline
=
False
)
option_ctr
+=
1
message
=
await
channel
.
send
(
""
,
embed
=
embed
)
if
not
result
:
for
i
in
range
(
0
,
len
(
self
.
answers
)):
await
message
.
add_reaction
(
OPTIONS
[
i
])
await
message
.
add_reaction
(
"
🗑️
"
)
await
message
.
add_reaction
(
"
🛑
"
)
async
def
close_poll
(
self
):
await
self
.
send_poll
(
self
.
message
.
channel
,
result
=
True
)
await
self
.
delete_poll
()
async
def
delete_poll
(
self
):
await
self
.
message
.
delete
()
def
get_reaction
(
self
,
reaction
):
if
self
.
message
:
reactions
=
self
.
message
.
reactions
for
react
in
reactions
:
if
react
.
emoji
==
reaction
:
return
react
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment