Reputation: 1
I'm trying to create a discord bot, that when you type >contract, it creates a new private channel & adds the message author, and the Admin role into the new channel. I have this code so far, but when trying to run the command, the bot does nothing. Not sure where my problem is coming from, if someone could take a look and share some insight? tia
import discord
from discord.ext import commands
from discord.utils import get
Token = '..'
client = discord.Client()
bot = commands.Bot(command_prefix='>')
@client.event
async def on_ready():
print('We have logged on as {0.user}'.format(client))
@bot.command()
async def contract(ctx, *, name=None):
if ctx.author == client.user:
return
guild = ctx.guild
member = ctx.author
admin_role = get(guild.roles, name="Admin")
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True),
admin_role: discord.PermissionOverwrite(read_messages=True)
}
if name == None:
await ctx.channel.send('Sorry, but you have to insert a name. Try again, but do it like this: `>contract [channel name]`')
else:
await ctx.guild.create_text_channel(name, overwrites=overwrites)
await ctx.channel.send(f"Created a channel named {name}")
client.run(Token)
Upvotes: 0
Views: 189
Reputation: 169338
Take a close look at your indentation.
The code in contract
is mostly inside the if ctx.author == client.user: return
block, and as you should know, no code after return
is executed. (Good IDEs and editors warn you about unreachable code.)
You might have a better time with something like this – handle exceptional cases and errors first and do the real work at the end of your function.
@bot.command()
async def contract(ctx, *, name=None):
if ctx.author == client.user:
return
if not name:
await ctx.channel.send(
"Sorry, but you have to insert a name. "
"Try again, but do it like this: `>contract [channel name]`"
)
return
guild = ctx.guild
member = ctx.author
admin_role = get(guild.roles, name="Admin")
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True),
admin_role: discord.PermissionOverwrite(read_messages=True),
}
await ctx.guild.create_text_channel(name, overwrites=overwrites)
await ctx.channel.send(f"Created a channel named {name}")
Upvotes: 0