【发布时间】:2021-06-12 00:54:22
【问题描述】:
我目前正在开发 discord.py 机器人,该机器人将用于管理 discord 服务器上的许可证。 目前卡在成功兑换代码后如何添加角色。 我在我的不和谐服务器中添加了 3 个角色(1 天、7 天、30 天), 如何让机器人为成功兑换许可证的用户添加角色?
这是我现在的代码:
import discord
import random
import re
from discord.ext import commands
from discord.utils import get
client = discord.Client()
l1d = open('licenses1d', 'r').read().splitlines()
l7d = open('licenses7d', 'r').read().splitlines()
l30d = open('licenses30d', 'r').read().splitlines()
def license_gen1d():
a = 'qwertzuiopasdfghjklyxcvbnm1234567890'
license = ''
while True:
while len(license) < 30:
character = random.choice(a)
license += character
if len(license) == 30:
with open('licenses1d', 'a') as f:
f.writelines(license + '\n')
f.close()
print('Successfuly Generated 1 day License: ' + license)
return(license)
def license_gen7d():
a = 'qwertzuiopasdfghjklyxcvbnm1234567890'
license = ''
while True:
while len(license) < 30:
character = random.choice(a)
license += character
if len(license) == 30:
with open('licenses7d', 'a') as f:
f.write(license + '\n')
f.close()
print('Successfuly Generated 7 days License: ' + license)
return(license)
def license_gen30d():
a = 'qwertzuiopasdfghjklyxcvbnm1234567890'
license = ''
while True:
while len(license) < 30:
character = random.choice(a)
license += character
if len(license) == 30:
with open('licenses30d', 'a') as f:
f.write(license + '\n')
f.close()
print('Successfuly Generated 30 days License: ' + license)
return(license)
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$test'):
await message.channel.send('test!')
if message.content.startswith('$generate 1d'):
await message.channel.send('Generating license for 1 day')
license = license_gen1d()
await message.channel.send(license)
if message.content.startswith('$generate 7d'):
await message.channel.send('Generating license for 7 days')
license = license_gen7d()
await message.channel.send(license)
if message.content.startswith('$generate 30d'):
await message.channel.send('Generating license for 30 days')
license = license_gen30d()
await message.channel.send(license)
if message.content.startswith('$redeem'):
rcode = re.findall('redeem (.*)', message.content)[0]
if rcode in l1d:
await message.channel.send('Successfully Redeemed Code for 1 day use!')
elif rcode in l7d:
await message.channel.send('Successfully Redeemed Code for 7 day use!')
elif rcode in l30d:
await message.channel.send('Successfully Redeemed Code for 30 day use!')
else:
await message.channel.send('Invalid code!')
client.run('token')
【问题讨论】:
-
我不确定如何帮助您解决这个主题,但我不禁注意到您多次声明
a具有相同的值。只需将其设为全局变量即可。 -
另外我建议使用命令来执行操作,而不是检查每条消息。它使创建别名和角色检查变得更加容易。
标签: python python-3.x discord discord.py