Reputation: 27
import discord
from discord.ext import commands
TOKEN = 'My Token'
command_prefix = '='
bot = commands.Bot(command_prefix = command_prefix)
announcement_channel_set = ""
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
@bot.command()
async def announcement(announcement_channel_set, announcement_text):
if announcement_channel_set == "":
await bot.send(f"@everyone {announcement_text}")
else:
channel = announcement_channel_set
await channel.send(f"@everyone {announcement_text}")
@bot.command()
async def changeannouncementchannel(ctx, change: discord.TextChannel):
global announcement_channel_set
announcement_channel_set = bot.get_channel(change)
await ctx.send(f"Announcement Channel Changed To #{change}")
bot.run(TOKEN)
This is my whole program, what I don't get is why when I change announcement_channel_set as a global variable, it doesn't change the channel I want the announcement to go into when I do =announcement {message}
Upvotes: 1
Views: 342
Reputation: 468
You have a couple things a bit off with your code.
In your function announcement
, what you do if announcement_channel_set == ""
is you use bot.send
, which does not work in the discord.py rewrite. We need to change the first parameter in the function to be ctx
and not announcement_channel_set
.
In your 2nd function changeannouncementchannel
, you already type hint change
to be an instance of discord.TextChannel
, so you don't need to do the function bot.get_channel(change)
, instead just change
.
I changed announcement_channel_set
to a bot variable, so we don't have to use global.
I used change.mention
instead of #{change}
so that it mentions the channel and the blue text appears.
import discord
from discord.ext import commands
TOKEN = 'My Token'
command_prefix = '='
bot = commands.Bot(command_prefix = command_prefix)
bot.announcement_channel = ""
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
@bot.command()
async def announcement(ctx, announcement_text):
if bot.announcement_channel == "":
await ctx.send(f"@everyone {announcement_text}")
else:
await bot.announcement_channel.send(f"@everyone {announcement_text}")
@bot.command()
async def changeannouncementchannel(ctx, change: discord.TextChannel):
bot.announcement_channel = change
await ctx.send(f"Announcement Channel Changed To {change.mention}")
Upvotes: 2
Reputation: 1318
When we're coding, we don't typically use global variables
, they cause issues and are generally difficult to manage. Rather, you can use a class
with assigned properties
that you can access from anywhere in your code. I've re-written parts of your code to adjust for these suggestions that will hopefully help you down the road.
import discord
class Announcement_Channel:
@property
def channel(self):
return self._channel
def __init__(self, channel):
self._channel = channel
announcement_channel = Announcement_Channel(None)
@bot.command()
async def announcement(ctx, announcement_text):
if announcement_channel.channel is None:
await ctx.send(f"@everyone {announcement_text}")
else:
await announcement_channel.channel.send(f"@everyone {announcement_text}")
@bot.command()
async def set_announcement_channel(ctx, channel: discord.TextChannel):
announcement_channel._channel = channel
await ctx.send(f"Announcement Channel Changed To {channel.mention}")
Upvotes: 0