I'm currently working on discord.py bot that'll be used to manage licenses on discord server. Currently stuck on how to add role after successful redeem of code. I added 3 roles in my discord server(1Day, 7Days, 30Days), how do I make bot to add role to user who successfully redeemed the license?
Here's my code for now:
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
multiple times with the same value. Just make it a global variable. - thethiny