Skip to content

Instantly share code, notes, and snippets.

@minibox24
Created April 23, 2021 05:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save minibox24/60acebe6322da42a16cc3822f2b747fe to your computer and use it in GitHub Desktop.
Save minibox24/60acebe6322da42a16cc3822f2b747fe to your computer and use it in GitHub Desktop.
discord button poll bot
import discord
from discord.ext import commands
from discord.http import Route
import uuid
bot = commands.Bot(command_prefix='`')
bot.poll_data = {}
def make_buttons(tag, data):
splited_data = [data[i * 5:(i + 1) * 5] for i in range((len(data) + 5 - 1) // 5 )]
components = []
for i in splited_data:
buttons = []
for j in i:
buttons.append({
'type': 2,
'style': 1,
'custom_id': f'{tag}.{j["id"]}',
'label': j['name']
})
components.append({
'type': 1,
'components': buttons
})
return components
@bot.command('투표')
async def poll(ctx, title, *names):
poll_id = uuid.uuid4().hex
data = []
bot.poll_data[poll_id] = {
'title': title,
'items': {}
}
for i in names:
item_id = uuid.uuid4().hex
bot.poll_data[poll_id]['items'][f'{poll_id}.{item_id}'] = {
'name': i,
'users': []
}
data.append({ 'name': i, 'id': item_id })
embed = discord.Embed(
title=title,
description="\n".join(map(lambda x: f'`{x}` : 0표', names)),
color=0x58D68D
)
embed.set_footer(text='Experiments에서 Desktop Bot UI Kit Buttons를 활성화 해주세요.')
route = Route('POST', '/channels/{channel_id}/messages', channel_id=ctx.channel.id)
await bot.http.request(route, json={
'embed': embed.to_dict(),
'components': make_buttons(poll_id, data)
})
@bot.event
async def on_socket_response(msg):
if msg['t'] != 'INTERACTION_CREATE': return
full_id = msg['d']['data']['custom_id']
poll_id = full_id.split('.')[0]
if not bot.poll_data.get(poll_id): return
data = bot.poll_data[poll_id]
user_id = msg['d']['member']['user']['id']
if user_id in data['items'][full_id]['users']:
bot.poll_data[poll_id]['items'][full_id]['users'].remove(user_id)
else:
bot.poll_data[poll_id]['items'][full_id]['users'].append(user_id)
embed = msg['d']['message']['embeds'][0]
content = "\n".join(map(lambda x: f'`{data["items"][x]["name"]}` : {len(data["items"][x]["users"])}표', data['items']))
embed['description'] = content
route = Route('PATCH', '/channels/{channel_id}/messages/{message_id}', channel_id=msg['d']['channel_id'], message_id=msg['d']['message']['id'])
await bot.http.request(route, json={
'embed': embed,
'components': msg['d']['message']['components']
})
bot.run('')
@EmptyFamily01
Copy link

신기하네요

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment